package com.example.blog_system_ssm.controller;

import com.example.blog_system_ssm.common.AjaxResult;
import com.example.blog_system_ssm.common.SensitiveWordUtil;
import com.example.blog_system_ssm.common.SessionUtil;
import com.example.blog_system_ssm.common.UserThreadLocal;
import com.example.blog_system_ssm.model.ArticleInfo;
import com.example.blog_system_ssm.model.UserInfo;
import com.example.blog_system_ssm.service.DraftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/draft")
public class DraftController {
    @Autowired
    private DraftService draftService;
    @Autowired
    private SensitiveWordUtil sensitiveWordUtil;
    @RequestMapping("/add")
    public int add(String title,String content){
        UserInfo userInfo= UserThreadLocal.get();
        //敏感词屏蔽
        if(sensitiveWordUtil.contains(title)||sensitiveWordUtil.contains(content)){
            title=sensitiveWordUtil.replaceSensitiveWord(title,'*',2);
            content=sensitiveWordUtil.replaceSensitiveWord(content,'*',2);
        }
        if(userInfo!=null&&userInfo.getId()>0){
            return draftService.add(userInfo.getId(),title,content);
        }
        return -1;
    }

    @RequestMapping("/mydraftlist")
    public List<ArticleInfo> getMyDraftList(){
        UserInfo userInfo= UserThreadLocal.get();
        if(userInfo!=null){
            return draftService.getMyDraftList(userInfo.getId());
        }
        return null;
    }

    @RequestMapping("/artdel")
    public Object delete(Long aid){
        UserInfo userInfo= UserThreadLocal.get();
        if(userInfo!=null){
            if(draftService.delete(aid,userInfo.getId())==1){
                return AjaxResult.success("删除成功");
            }
        }
        return AjaxResult.fail(-1,"","删除失败，文章不存在");
    }

    @RequestMapping("/detail")
    public Object detail(Long aid){
        UserInfo userInfo= UserThreadLocal.get();
        if(userInfo!=null){
            if(draftService.getDetail(aid,userInfo.getId())!=null){
                return AjaxResult.success(draftService.getDetail(aid,userInfo.getId()));
            }
        }
        return AjaxResult.fail(-1,"查询失败");
    }

    @RequestMapping("/update")
    public int update(Long aid, String title, String content){
        UserInfo userInfo= UserThreadLocal.get();
        //敏感词屏蔽
        if(sensitiveWordUtil.contains(title)||sensitiveWordUtil.contains(content)){
            title=sensitiveWordUtil.replaceSensitiveWord(title,'*',2);
            content=sensitiveWordUtil.replaceSensitiveWord(content,'*',2);
        }
        if(userInfo!=null&&userInfo.getId()>0&&aid>0){
            return draftService.update(aid,userInfo.getId(),title,content);
        }
        return -1;
    }

    @RequestMapping("/submit")
    public int submit(Long aid,String title,String content){
        UserInfo userInfo= UserThreadLocal.get();
        if(userInfo!=null&&userInfo.getId()>0&&aid>0){
            return draftService.submit(userInfo.getId(),aid,title,content);
        }
        return -1;
    }
}
